//
//  VCForActivityCell.m
//  echo
//
//  Created by dllo on 16/3/8.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "VCForActivityCell.h"
#import "NetworkingHandler.h"
#import "ModelForActivityCellVC.h"
#import "ModelForActivityCellVCSectionPrize.h"
#import <DKNightVersion.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "CellForVCForActivityCell.h"

@interface VCForActivityCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UIImageView *imageForPic;
@property (nonatomic, retain)UILabel *label;
@property (nonatomic, retain)ModelForActivityCellVC *modelForActivity;
@property (nonatomic, retain)NSMutableArray *arrForPrize;
@property (nonatomic, retain)UILabel *line;
@property (nonatomic, retain)UILabel *labelForSound;
@property (nonatomic, retain)UILabel *labelForUser;
@property (nonatomic, retain)UIButton *buttonForBack;
@property (nonatomic, retain)UIView *viewForBack;
@property (nonatomic, retain)UITableView *tableView;
@end

@implementation VCForActivityCell

- (void)dealloc{
    [_id release];
    [_imageForPic release];
    [_label release];
    [_modelForActivity release];
    [_arrForPrize release];
    [_line release];
    [_labelForSound release];
    [_labelForUser release];
    [_buttonForBack release];
    [_viewForBack release];
    self.tableView.delegate = nil;
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    [self handleData];
}

- (void)handleData{
    NSString *str = @"http://echosystem.kibey.com/event/get?page=1&order=2&event_id=";
    NSString *url = [str stringByAppendingFormat:@"%@&limit=10", self.id];
    [NetworkingHandler networkingGetWithUrl:url dicForValue:@{@"x-v":@"20160209",@"x-c":@"3",@"Cookie":@"PHPSESSID=5lskjp19poc82pst870o96rtc6"} completeHandle:^(id result) {
        self.modelForActivity = [[ModelForActivityCellVC alloc]init];
        NSDictionary *dic = [result objectForKey:@"result"];
        NSDictionary *dic2 = [dic objectForKey:@"event"];
        [self.modelForActivity setValuesForKeysWithDictionary:dic2];

        self.arrForPrize = [NSMutableArray array];
        NSDictionary *dic3 = [result objectForKey:@"result"];
        NSArray *arr = [dic3 objectForKey:@"prize"];
        for (NSDictionary *tempDic in arr) {
            ModelForActivityCellVCSectionPrize *model = [[ModelForActivityCellVCSectionPrize alloc]init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.arrForPrize addObject:model];
        }
        [self createViews];
    }];
}

- (void)createViews{
    self.view.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    self.viewForBack = [[UIView alloc]init];
    self.viewForBack.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    
    UILabel *labelForActivity = [[UILabel alloc]init];
    labelForActivity.text = @"echo回声活动";
    labelForActivity.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    labelForActivity.frame = CGRectMake(self.view.frame.size.width / 2 - 75 * WIDTH, 18 * HEIGHT, 150 * WIDTH, 50 * HEIGHT);
    [self.view addSubview:labelForActivity];
    labelForActivity.textAlignment = 1;
    
    self.buttonForBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.buttonForBack];
    self.buttonForBack.frame = CGRectMake(8 * WIDTH, 28 * HEIGHT, 30 * WIDTH, 30 * WIDTH);
    [self.buttonForBack setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [self.buttonForBack addTarget:self action:@selector(handleBackForActivityCell) forControlEvents:UIControlEventTouchUpInside];
    
    self.imageForPic = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * HEIGHT)];
    [self.viewForBack addSubview:self.imageForPic];
    [self.imageForPic release];
    [self.imageForPic sd_setImageWithURL:[NSURL URLWithString:self.modelForActivity.pic]];
    [self.imageForPic sd_setImageWithURL:[NSURL URLWithString:self.modelForActivity.pic]];
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(5,200 * HEIGHT, self.view.frame.size.width - 10, [[self class] heightForLabel:self.modelForActivity.content] + 65 * HEIGHT)];
    [self.viewForBack addSubview:self.label];
    [self.label release];
    self.label.numberOfLines = 0;
    self.label.text = self.modelForActivity.content;
    self.label.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    
    self.line = [[UILabel alloc]initWithFrame:CGRectMake(15 * WIDTH, self.label.frame.size.height - 52 * HEIGHT, self.view.frame.size.width - 30 * WIDTH, 1 * HEIGHT)];
    [self.label addSubview:self.line];
    [self.line release];
    self.line.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1],[UIColor whiteColor]);
    
    self.labelForSound = [[UILabel alloc]initWithFrame:CGRectMake(30 * WIDTH, self.label.frame.size.height - 45 * HEIGHT, 120 * WIDTH, 40 * HEIGHT)];
    [self.label addSubview:self.labelForSound];
    [self.labelForSound release];
    self.labelForSound.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    NSString *str = [self.modelForActivity.sound_count stringValue];
    NSString *strForSoundCount = [str stringByAppendingString:@"个声音"];
    
    NSRange range1 = [strForSoundCount rangeOfString:str];
    NSMutableAttributedString *attribue1 = [[NSMutableAttributedString alloc]initWithString:strForSoundCount];
    [attribue1 addAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor]} range:range1];
    [self.labelForSound setText:strForSoundCount];
    [self.labelForSound setAttributedText:attribue1];
    [attribue1 release];
    self.labelForSound.textAlignment = 1;
    
    self.labelForUser = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 150 * WIDTH, self.label.frame.size.height - 45 * HEIGHT, 120 * WIDTH, 40 * HEIGHT)];
    [self.label addSubview:self.labelForUser];
    [self.labelForUser release];
    self.labelForUser.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    NSString *str2 = [self.modelForActivity.user_count stringValue];
    NSString *strForUserCount = [str2 stringByAppendingString:@"人参加"];
    NSRange range2 = [strForUserCount rangeOfString:str2];
    NSMutableAttributedString *attribue2 = [[NSMutableAttributedString alloc]initWithString:strForUserCount];
    [attribue2 addAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor]} range:range2];
    [self.labelForUser setText:strForUserCount];
    [self.labelForUser setAttributedText:attribue2];
    [attribue2 release];
    self.labelForUser.textAlignment = 1;
    
    self.viewForBack.frame = CGRectMake(0, 64, self.view.frame.size.width, 200 * HEIGHT + [[self class] heightForLabel:self.modelForActivity.content] + 65 * HEIGHT);

    [self createTableView];
}

- (void)createTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    self.tableView.tableHeaderView = self.viewForBack;
    [self.tableView registerClass:[CellForVCForActivityCell class] forCellReuseIdentifier:@"poolForCellForVCForActivityCell"];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *viewForHeaderBack = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    viewForHeaderBack.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1],[UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    UILabel *labelForHeader = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, 40)];
    [viewForHeaderBack addSubview:labelForHeader];
    labelForHeader.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    labelForHeader.text = @"      活动奖品";
    labelForHeader.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    labelForHeader.font = [UIFont fontWithName:@"Arial" size:17.0];
    return viewForHeaderBack;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrForPrize.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50 * HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90 * HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01 * HEIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellForVCForActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForCellForVCForActivityCell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    ModelForActivityCellVCSectionPrize *model = [self.arrForPrize objectAtIndex:indexPath.row];
    [cell passModel:model];
    return cell;
}

- (void)handleBackForActivityCell{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

+ (CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"ArialUnicodeMS" size:17.1]} context:nil];
    
    return rect.size.height;
}

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
